<?php
class CSVImporter extends BaseImporter implements ImporterClass {
    public function validate($params) {
        $error = false;
        $errorMsg = "";
        $result = serviceSuccessful(array());

        while (!feof($this->fileHandler)) {
            $row = $this->getNextRow();
            if ($row == "") { continue; }
            $columns = explode(";", $row);
            if ($params['type'] == "1" || $params['type'] == "2") {
                if (count($columns) < 10) {
                    $error = true;
                    $errorMsg = "Error in line " . ($this->currentRow-1) . " - invalid column number";
                    break;
                }
            } else if ($params['type'] == "5") {
                if (count($columns) < 10) {
                    $error = true;
                    $errorMsg = "Error in line " . ($this->currentRow-1) . " - invalid column number";
                    break;
                }
            } else if ($params['type'] == "6") {
                if (count($columns) < 16) {
                    $error = true;
                    $errorMsg = "Error in line " . ($this->currentRow-1) . " - invalid column number";
                    break;
                }
            }
        }
        if ($error) {
            $result = serviceFailure(array(), $errorMsg);
        }
        return $result;
    }

    public function getColumn($row, $columnName) {
        $row = str_replace("\"", "", $row);
        $row = str_replace("'", "''", $row);
        $columns = explode(";", $row);
        return $columns[array_search($columnName, $this->columnList)];
    }

    public function getNextRow() {
        if ($this->currentRow == 0) {   // az elso sor a fejlec
            parent::getNextRow();
            $this->step();
        }
        $result = parent::getNextRow();
        $this->step();
        return $result;
    }
}

?>
